﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Data;
public partial class Admin_News_management : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        cldLastedit.SelectedDate = DateTime.Today;
        if (Convert.ToString(Session["Dangnhap"]) == "")
            Response.Redirect("~/Admin/Login.aspx");

        else
            Response.Write("Xin chào " + Convert.ToString(Session["Dangnhap"]));
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Admin/Add_News.aspx");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        
        
        SqlConnection conn = new SqlConnection
        (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        string Update = "Update tblNews Set newsContent=@con ,published=@pub ,lastEditedUserId=@lastuser ,lastEditedDate=@lastdate where (newsTitle=@title) ";
        //Update += "@con,@pub,@lastuser,@lastdate,@title";
        SqlCommand sqlCmd = new SqlCommand(Update, conn);
        sqlCmd.Parameters.AddWithValue("@title",txtTitle.Text );
        sqlCmd.Parameters.AddWithValue("@con",FCKeditor1.Value );
        sqlCmd.Parameters.AddWithValue("@pub",ddlPublish.SelectedIndex );
        sqlCmd.Parameters.AddWithValue("@lastuser", ddlEditUser.SelectedIndex);
        sqlCmd.Parameters.AddWithValue("@lastdate", cldLastedit.SelectedDate);
        conn.Open();
        sqlCmd.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("~/Admin/News_management.aspx");
        

    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtTitle.Text = HttpUtility.HtmlDecode(GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text); ;
        FCKeditor1.Value =HttpUtility.HtmlDecode(GridView1.Rows[GridView1.SelectedIndex].Cells[2].Text) ;
        ddlPublish.Text = (GridView1.Rows[GridView1.SelectedIndex].Cells[3].Text);
        //ddlCreatorID.Text=(GridView1.Rows[GridView1.SelectedIndex].Cells[4].Text);
        //ddlEditUser.Text=(GridView1.Rows[GridView1.SelectedIndex].Cells[6].Text);
        //cldLastedit.TodaysDate =Convert.ToDateTime (GridView1.Rows[GridView1.SelectedIndex].Cells[7].Text);
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection
        (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        string Delete = "Delete from tblNews  where newsTitle=@title ";
        //Update += "@con,@pub,@lastuser,@lastdate,@title";
        SqlCommand sqlCmd = new SqlCommand(Delete, conn);
        sqlCmd.Parameters.AddWithValue("@title", txtTitle.Text);
        conn.Open();
        sqlCmd.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("~/Admin/News_management.aspx");
         //News_management.aspx
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Label1.Visible = true;
        Label2.Visible = true;
        Label3.Visible = true;
        Label6.Visible = true;
        Label7.Visible = true;
        Label8.Visible = true;
        txtTitle.Visible = true;
        FCKeditor1.Visible = true;
        cldLastedit.Visible = true;
        ddlEditUser.Visible = true;
        ddlPublish.Visible = true;
        Button2.Visible = true;
        
    }
}
